package com.swt.shoppingmall.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swt.shoppingmall.R;

/**
 * 购物车数量增加和减少
 * Created by swt on 2016/12/27.
 */

public class NumberAddSubView extends LinearLayout implements View.OnClickListener {

    private final ImageView btn_sub;
    private final TextView tv_count;
    private final ImageView btn_add;
    private int value = 1;//当前值
    private int minValue = 1;//最小值
    private int maxValue = 10;//最大值

    public NumberAddSubView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(context, R.layout.number_add_sub_layout, this);
        btn_sub = (ImageView) findViewById(R.id.btn_sub);
        tv_count = (TextView) findViewById(R.id.tv_count);
        btn_add = (ImageView) findViewById(R.id.btn_add);

        btn_sub.setOnClickListener(this);
        btn_add.setOnClickListener(this);

        int value = getValue();
        tv_count.setText(value + "");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sub://减
                sunNumber();
                break;
            case R.id.btn_add://加
                addNumber();
                break;
        }
    }

    private void addNumber() {
        if (value < maxValue) {
            value++;
        }
        setValue(value);
        if (onNumberChangerListener != null) {
            onNumberChangerListener.onNumberChange(value);
        }
    }

    private void sunNumber() {
        if (value > minValue) {
            value--;
        }
        setValue(value);

        if (onNumberChangerListener != null) {
            onNumberChangerListener.onNumberChange(value);
        }
    }

    public int getValue() {
        String valueStr = tv_count.getText().toString().trim();
        if (!TextUtils.isEmpty(valueStr)) {
            value = Integer.parseInt(valueStr);
        }
        return value;
    }

    public void setValue(int value) {
        this.value = value;
        tv_count.setText(value + "");
    }

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    /**
     * 当数量发生变化的时候回调
     */
    public interface OnNumberChangerListener {
        /**
         * 当数据发生变化的时候回调
         *
         * @param value
         */
        void onNumberChange(int value);
    }

    private OnNumberChangerListener onNumberChangerListener;

    /**
     * 设置数据变化的监听
     *
     * @param onNumberChangerListener
     */
    public void setOnNumberChangerListener(OnNumberChangerListener onNumberChangerListener) {
        this.onNumberChangerListener = onNumberChangerListener;
    }
}
